<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    var ws = new WebSocket('ws://127.0.0.1/ws');
    ws.onopen = function (evt) {  //绑定连接事件
        console.log("Connection open ...");
        ws.send('138243');
    };

    ws.onmessage = function (evt) {//绑定收到消息事件
        console.log("Received Message: " + evt.data);
    };

    ws.onclose = function (evt) { //绑定关闭或断开连接事件
        console.log("Connection closed.");
    };

    window.onbeforeunload = function(event) {
        ws.onclose =function(){};
        ws.close();
    }

</script>
</body>
</html>